//
//  SISSOnlineSignatureContractVerifyShowViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/8.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSOnlineSignatureContractVerifyShowViewController.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSHouseProgressModel.h"
#import "SISSHouseProgressRecordInfoModel.h"
#import "SISSPassengerProgressModel.h"
#import "SISSPassengerProgressRecordInfoModel.h"
#import "SISSIntermediaryContractModel.h"
#import "SISSContractVerifyInfoModel.h"

@interface SISSOnlineSignatureContractVerifyShowViewController ()

@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UILabel *tipLabel;
@property (weak, nonatomic) UILabel *contentLabel;
@property (weak, nonatomic) UIButton *bottomButton;

@end

@implementation SISSOnlineSignatureContractVerifyShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"系统核验网签合同";
    
    [self requestVerifyInfoData];
}

- (void)setTipStr:(NSString *)tipStr {
    _tipStr = tipStr;
    self.tipLabel.text = tipStr;
}

- (void)setIsVerifySuccess:(BOOL)isVerifySuccess {
    _isVerifySuccess = isVerifySuccess;
    if (self.isVerifySuccess) {
        self.imageView.image = [UIImage imageNamed:@"checkSelected"];
        self.tipLabel.text = @"网签合同校验成功";
        self.contentLabel.text = @"三方已完成网签合同，买卖双方确认已签署后就可以继续后面的业务。";
    } else {
        self.imageView.image = [UIImage imageNamed:@"X"];
        self.tipLabel.text = @"网签合同校验不通过";
        self.contentLabel.text = @"";
    }
    self.bottomButton.hidden = !isVerifySuccess;
}

- (void)requestVerifyInfoData {
    
    NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : self.passengerProgressRecordInfoModel.ahbpId;
    NSString *adbpId = self.passengerProgressModel ? self.passengerProgressModel.adbpId : self.houseProgressRecordInfoModel.adbpId;
    NSInteger houseType = self.houseProgressModel ? 1 : self.passengerProgressRecordInfoModel.tradeBusinessType;
    NSInteger demandType = self.passengerProgressModel ? 1 : self.houseProgressRecordInfoModel.tradeBusinessType;
    if (houseType == 2) ahbpId = self.passengerProgressRecordInfoModel.ahrId;
    if (demandType == 2) adbpId = self.houseProgressRecordInfoModel.abdId;
    
    WeakSelf
    [kNetworkManager requestVerifyInfoWithAhbpId:ahbpId adbpId:adbpId success:^(SISSContractVerifyInfoModel *verifyInfoModel) {
        if (verifyInfoModel.isVerify == 1) {
            weakSelf.imageView.image = [UIImage imageNamed:@"checkSelected"];
            self.tipLabel.text = @"网签合同校验成功";
            self.contentLabel.text = @"三方已完成网签合同，买卖双方确认已签署后就可以继续后面的业务。";
            NSString *bottomStr = @"";
            if (verifyInfoModel.sellerState == 0 && verifyInfoModel.buyerState == 0) {
                bottomStr = @"等待买卖双方确认网签状态";
            } else if ((verifyInfoModel.sellerState == 1 && verifyInfoModel.buyerState == 0)) {
                bottomStr = @"等待买方确认网签状态";
            } else if ((verifyInfoModel.sellerState == 0 && verifyInfoModel.buyerState == 1)) {
                bottomStr = @"等待卖方确认网签状态";
            } else {
                 if (verifyInfoModel.sellerState == 2 && verifyInfoModel.buyerState != 2) {
                    bottomStr = @"卖方拒绝网签";
                } else if (verifyInfoModel.sellerState != 2 && verifyInfoModel.buyerState == 2) {
                    bottomStr = @"买方拒绝网签";
                } else if (verifyInfoModel.sellerState == 2 && verifyInfoModel.buyerState == 2) {
                    bottomStr = @"买卖双方拒绝网签";
                } else {
                    self.bottomButton.hidden = YES;
                }
            }
            [self.bottomButton setTitle:bottomStr forState:UIControlStateNormal];
        } else if (verifyInfoModel.isVerify == 2) {
            self.imageView.image = [UIImage imageNamed:@"X"];
            self.tipLabel.text = @"网签合同校验不通过";
            self.contentLabel.text = verifyInfoModel.reason;
            self.bottomButton.hidden = YES;
        }
     
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:self.isVerifySuccess ? @"checkSelected" : @"X"];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    UILabel *tipLabel = [UILabel yyr_labelWithText:self.isVerifySuccess ? @"网签合同校验成功" : @"网签合同校验不通过" fontSize:16 textColor:MAIN_TEXT_COLOR alignment:NSTextAlignmentCenter];
    tipLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    UILabel *contentLabel = [UILabel yyr_labelWithText:@"" fontSize:16 textColor:MAIN_TEXT_COLOR alignment:NSTextAlignmentCenter];
    contentLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:contentLabel];
    self.contentLabel = contentLabel;
    self.contentLabel.text = self.isVerifySuccess ? @"三方已完成网签合同，买卖双方确认已签署后就可以继续后面的业务。" : @" ";
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [bottomButton setTitle:@"等待买卖双方确认网签状态" forState:UIControlStateNormal];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomButton setBackgroundColor:MAIN_COLOR];
    bottomButton.userInteractionEnabled = NO;
    bottomButton.hidden = !self.isVerifySuccess;
    [self.view addSubview:bottomButton];
    self.bottomButton = bottomButton;
    NSString *bottomStr;
    if (self.isVerifySuccess) {
        bottomStr = @"等待确认网签状态";
    }
    [self.bottomButton setTitle:bottomStr forState:UIControlStateNormal];
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom).offset(4 * globelMargin);
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 0.2, SCREEN_WIDTH * 0.2));
    }];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imageView.mas_bottom).offset(2 * globelMargin);
        make.centerX.equalTo(weakSelf.imageView);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tipLabel.mas_bottom).offset(2 * globelMargin);
        make.centerX.equalTo(weakSelf.view);
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
    }];
    
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.view);
        make.height.mas_equalTo(globelMargin * globelMargin);
    }];
    
}

@end
